define(function (require, exports, module) {

    // 通过 require 引入依赖
    // var $ = require('jquery');
    require('common');
    require('sui');
    var template = require('template');
    dateFormat(template);
    var rows = 20;

    var groupId = $('#groupId').val();

    function initData() {
        getDelChannelData(1, rows, groupId);
        getAddChannelData(1, rows, groupId);
        getDelUserData(1, rows, groupId);
        getAddUserData(1, rows, groupId);
    }

    $('#form-group').validate({
        rules: {
            groupName: {
                required: true,
                minlength: 2,
                maxlength: 20
            }
        },
        messages: {
            groupName: "请填写"
        },
        success: function () {
            updateGroup();
            return false;
        }
    });

    $(document).on('click', '.check-add-channel', function () {
        if ($(this).is(":checked")) {
            $('input[name="channel-id-add"]').prop("checked", true);//全选
        } else {
            $('input[name="channel-id-add"]').prop("checked", false);//取消全选
        }
    }).on('click', '.query-channel-add', function () {
        getAddChannelData(1, rows, 0, groupId);
    }).on('click', '.add-channel', function () {
        var inputs = $('input[name="channel-id-add"]');
        var ids = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                // ids[i] = parseInt($(item).val());
                ids.push(parseInt($(item).val()));
            }
        });
        if (ids.length == 0) {
            toastInfo('请选择要添加的渠道');
            return;
        }
        $.post('/group/addChannel', {ids: JSON.stringify(ids), groupId: groupId}, function (data) {
            switch (data.code) {
                case 0:
                    toastSuccess('添加成功');
                    getDelChannelData(1, rows, groupId);
                    getAddChannelData(1, rows, groupId);
                    break;
                default :
                    toastError('添加失败');
            }
        });
    }).on('click', '.check-del-channel', function () {
        if ($(this).is(":checked")) {
            $('input[name="channel-id-del"]').prop("checked", true);//全选
        } else {
            $('input[name="channel-id-del"]').prop("checked", false);//取消全选
        }
    }).on('click', '.query-channel-del', function () {
        getDelChannelData(1, rows, groupId);
    }).on('click', '.del-channel', function () {
        var inputs = $('input[name="channel-id-del"]');
        var ids = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                // ids[i] = parseInt($(item).val());
                ids.push(parseInt($(item).val()));
            }
        });
        if (ids.length == 0) {
            toastInfo('请选择要添加的渠道');
            return;
        }
        $.post('/group/delChannel', {ids: JSON.stringify(ids), groupId: groupId}, function (data) {
            switch (data.code) {
                case 0:
                    toastSuccess('删除成功');
                    getDelChannelData(1, rows, groupId);
                    getAddChannelData(1, rows, groupId);
                    break;
                default :
                    toastError('删除失败');
            }
        });
    }).on('click', '.check-add-user', function () {
        if ($(this).is(":checked")) {
            $('input[name="group-user-id-add"]').prop("checked", true);//全选
            $('.tab-user-add tbody tr').addClass('tr-click');
        } else {
            $('input[name="group-user-id-add"]').prop("checked", false);//取消全选
            $('.tab-user-add tbody tr').removeClass('tr-click')
        }
    }).on('click', '.query-user-add', function () {
        getAddUserData(1, rows, groupId);
    }).on('click', '.add-user', function () {
        var inputs = $('input[name="group-user-id-add"]');
        var ids = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                ids.push(parseInt($(item).val()));
            }
        });
        if (ids.length == 0) {
            toastInfo('请选择要添加的账号');
            return;
        }
        $.post('/group/addUser', {ids: JSON.stringify(ids), groupId: groupId}, function (data) {
            switch (data.code) {
                case 0:
                    toastSuccess('添加成功');
                    getAddUserData(1, rows, groupId);
                    getDelUserData(1, rows, groupId);
                    break;
                default :
                    toastError('添加失败');
            }
        });
    }).on('click', '.check-del-user', function () {
        if ($(this).is(":checked")) {
            $('input[name="group-user-id-del"]').prop("checked", true);//全选
            $('#tab-user-del tbody tr').addClass('tr-click');
        } else {
            $('input[name="group-user-id-del"]').prop("checked", false);//取消全选
            $('#tab-user-del tbody tr').removeClass('tr-click')
        }
    }).on('click', '.query-user-del', function () {
        getDelUserData(1, rows, groupId);
    }).on('click', '.del-user', function () {
        var inputs = $('input[name="group-user-id-del"]');
        var ids = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                // ids[i] = parseInt($(item).val());
                ids.push(parseInt($(item).val()));
            }
        });
        if (ids.length == 0) {
            toastInfo('请选择要添加的账号');
            return;
        }
        $.post('/group/delUser', {ids: JSON.stringify(ids), groupId: groupId}, function (data) {
            switch (data.code) {
                case 0:
                    toastSuccess('删除成功');
                    initData();
                    break;
                default :
                    toastError('删除失败');
            }
        });
    });

    function updateGroup() {
        var groupName = $.trim($('#groupName').val());
        var groupLevel = $('#groupLevel').val();
        var groupType = $('#groupType').val();
        var groupRemark = $.trim($('#groupRemark').val());
        var data = {
            groupName: groupName,
            groupLevel: groupLevel,
            groupType: groupType,
            groupRemark: groupRemark,
            groupId: groupId
        };
        $('.update-group').prop('disabled', 'disabled');
        $.post('/group/update', data, function (o) {
            switch (o.code) {
                case 0:
                    toastSuccess('修改群组成功');
                    initData();
                    break;
                default:
                    toastError(o.msg);
                    $('.uodate-group').removeAttr('disabled');
            }
        })

    }

    function getAddChannelData(page, rows, groupId) {
        var channelId = $.trim($('#channelId2').val());
        var channelName = $.trim($('#channelName2').val());
        var data = {
            channelId: channelId,
            channelName: channelName,
            page: page,
            rows: rows,
            channelStatus: '0',
            groupChannelFlag: 0,
            groupId: groupId
        };
        $.post('/group/listChannel', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-channel-add', {list: list});
                    $('#tab-channel-add tbody').empty();
                    $('#tab-channel-add tbody').html(html);
                    $('.channel-add-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询群组数据失败');
            }
        });
    }

    function getDelChannelData(page, rows, groupId) {
        var channelId = $.trim($('#channelId1').val());
        var channelName = $.trim($('#channelName1').val());
        var data = {
            channelId: channelId,
            channelName: channelName,
            page: page,
            rows: rows,
            channelStatus: '0',
            groupChannelFlag: 1,
            groupId: groupId
        };
        $.post('/group/listChannel', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-channel-del', {list: list});
                    $('#tab-channel-del tbody').empty();
                    $('#tab-channel-del tbody').html(html);
                    $('.channel-del-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询群组数据失败');
            }
        });
    }

    function getAddUserData(page, rows, groupId) {
        var accountName = $.trim($('#accountName2').val());
        var data = {
            accountName: accountName,
            page: page,
            rows: rows,
            groupUserFlag: 0,
            groupId: groupId
        };
        $.post('/group/listUser', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-user-add', {list: list});
                    $('#tab-user-add tbody').empty();
                    $('#tab-user-add tbody').html(html);
                    $('.user-add-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    function getDelUserData(page, rows, groupId) {
        var accountName = $.trim($('#accountName1').val());
        var data = {
            accountName: accountName,
            page: page,
            rows: rows,
            groupUserFlag: 1,
            groupId: groupId
        };
        $.post('/group/listUser', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-user-del', {list: list});
                    $('#tab-user-del tbody').empty();
                    $('#tab-user-del tbody').html(html);
                    $('.user-del-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    $('.user-add-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getAddUserData(num, rows, groupId)
        }
    });

    $('.user-del-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getDelUserData(num, rows, groupId)
        }
    });

    $('.channel-del-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getDelChannelData(num, rows, groupId)
        }
    });

    $('.channel-add-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getAddChannelData(num, rows, groupId)
        }
    });

    function dateFormat(template) {
        template.helper('dateFormat', function (date, format) {
            date = new Date(date);
            var map = {
                "M": date.getMonth() + 1, //月份
                "d": date.getDate(), //日
                "h": date.getHours(), //小时
                "m": date.getMinutes(), //分
                "s": date.getSeconds(), //秒
                "q": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds() //毫秒
            };
            format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
                var v = map[t];
                if (v !== undefined) {
                    if (all.length > 1) {
                        v = '0' + v;
                        v = v.substr(v.length - 2);
                    }
                    return v;
                }
                else if (t === 'y') {
                    return (date.getFullYear() + '').substr(4 - all.length);
                }
                return all;
            });
            return format;
        });
    }

    initData();
});